<?php

namespace app\common\taglib;


use app\model\PortalArticle;
use app\model\SystemSettings;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\template\TagLib;

class Bb extends TagLib
{
    /**
     * 定义标签列表
     * @var array[]
     */
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'close' => ['attr' => 'time,format', 'close' => 0], //闭合标签，默认为不闭合
        'open' => ['attr' => 'name,type', 'close' => 1],
        'setting' => ['attr' => 'name', 'close' => 0],
        'menu' => ['attr' => 'pid', 'close' => 1],
        'article' => ['attr' => 'aid,tid,row,typeid,order,type,time'],
        'flink' => ['attr' => 'id, row', 'level' => 1],
    ];

    /**
     * @param array $tag
     * @param string $content
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function tagArticle(array $tag, string $content): string
    {
        $aid = $tag['aid'] ?? 0;
        $tid = $tag['tid'] ?? 0;
        $row = $tag['row'] ?? 99;
        $type = $tag['type'] ?? '';
        $order = $tag['order'] ?? 'create_time desc';
        $item = $tag['item'] ?? 'bb';
        $time = $tag['time'] ?? 0;

        //格式化数组变量
        $aid = $this->autoBuildVar($aid); //格式化数组变量
        $tid = $this->autoBuildVar($tid); //格式化数组变量

        //$tag_sql = \app\model\PortalArticle::getList($tid, $type, $row, $order);
        $str = '<?php ';
        if (empty($aid)) {
            $str .= ' $tag_sql = \app\model\PortalArticle::getList(' . $tid . ',"' . $type . '",' . $row . ',"' . $order . '"); ';
            $str .= ' ?>';
            $str .= ' {foreach $tag_sql as $key => $' . $item . '}';
            $str .= $content;
            $str .= ' {/foreach}';
        } else {
            $str .= ' $tag_sql = \app\model\PortalArticle::getArticle(' . $aid . '); ';
            $str .= ' $' . $item . ' = $tag_sql;';
            $str .= '?>';
            $str .= $content;
        }
        return $str;
    }

    /**
     * 获取网站栏目
     * @param $tag
     * @param $content
     * @return string
     * @author An Yang
     * @time 2020/8/9 21:26
     */
    public function tagMenu($tag, $content): string
    {
        $pid = $tag['pid'] ?? 0;
        //$list = PortalMenu::where('pid', '=', $pid)->field("name")->select();
        //View::assign('list',$list);

        $pid = $this->autoBuildVar($pid); //格式化数组变量

        $parse = '<?php ';
        $parse .= '$__LIST__ = app\model\PortalMenu::getMenu(' . $pid . ');';
        $parse .= ' ?>';
        $parse .= '{foreach $__LIST__  as $key => $bb}';
        $parse .= $content;
        $parse .= '{/foreach}';

        return $parse;
    }

    /**
     * 友情链接
     * @param array $tag 标签属性
     * @param string $content 标签内容
     * @return string
     */
    public function tagFlink(array $tag, string $content): string
    {
        $id = !empty($tag['id']) ? $tag['id'] : null;
        $row = !empty($tag['row']) ? $tag['row'] : 99;

        //$where = $tag['where'];
        $Str = '<?php ';
        $Str .= ' $tag_sql = \app\model\OperateFlink::name("operate_flink") -> limit(' . $row . ') -> cache(true) -> select(); ';
        $Str .= ' foreach($tag_sql as $bb): ';
        $Str .= '?>';
        //$Str .= $this->tpl->parse($content);
        $Str .= $content;

        $Str .= '<?php endforeach; ';
        $Str .= '?>';

        if (!empty($Str)) {
            return $Str;
        }
        return '';
    }

    /**
     * 读取系统配置标签
     * @param $tag
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author An Yang
     * @time 2020/8/9 20:43
     */
    public function tagSetting($tag): string
    {
        $name = $tag['name'];
        $setting = SystemSettings::cache()->find($name);
        $value = $setting['value'] ?? "";

        $parse = '<?php ';
        $parse .= 'echo "' . $value . '";';
        $parse .= ' ?>';
        return $parse;
    }

}